package com.fy.service.imbusiness.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fy.common.dto.BaseIdDto;
import com.fy.common.dto.GroupCreateDto;
import com.fy.common.dto.GroupInfoDto;
import com.fy.common.dto.MemberGroupInfoEditDto;
import com.fy.common.entity.SkImGroup;
import com.fy.common.entity.SkImGroupMember;
import com.fy.common.util.TokenUtil;
import com.fy.framework.common.model.R;
import com.fy.framework.component.config.LocaleMessage;
import com.fy.service.service.SkImGroupMemberService;
import com.fy.service.service.SkImGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhshuo
 * @date 2021/12/17 18:32
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "群组")
@RequestMapping("/group")
public class SkImGroupController {

    LocaleMessage localeMessage;

    SkImGroupService imGroupService;

    SkImGroupMemberService groupMemberService;

    /**
     * 群创建
     * @return
     */
    @ApiOperation("创建群")
    @PostMapping("create")
    public R<?> create(@RequestBody GroupCreateDto groupCreateDto){
        try {
            imGroupService.create(groupCreateDto);
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            log.error("error",e);
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 用户所有群获取
     * @return
     */
    @ApiOperation("用户所有群获取")
    @PostMapping("listByUser")
    public R<List<SkImGroup>> listByUser(){
        try {
            List<SkImGroupMember> groupMembers = groupMemberService.selectList(new EntityWrapper<SkImGroupMember>().setSqlSelect("sk_im_group_id").eq("sk_im_user_id", TokenUtil.getUserId()));
            Set<Long> collect = groupMembers.stream().map(SkImGroupMember::getSkImGroupId).collect(Collectors.toSet());
            return R.success(imGroupService.selectList(new EntityWrapper<SkImGroup>().in("id",collect).in("status", Arrays.asList(0,1))));
        } catch (Exception e) {
            log.error("error",e);
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 群信息获取
     * @return
     */
    @ApiOperation("群信息获取")
    @PostMapping("infoGet")
    public R<SkImGroup> infoGet(@RequestBody BaseIdDto baseIdDto ){
        try {
            return R.success(imGroupService.selectById(baseIdDto.getId()));
        } catch (Exception e) {
            log.error("error",e);
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 邀请进群
     *
     * @return
     */
    @PostMapping("inviteIntoGroup")
    @ApiOperation("邀请用户进群")
    @ApiImplicitParams({@ApiImplicitParam(name = "groupId", value = "邀请的群ID", required = true),
            @ApiImplicitParam(name = "userId", value = "被邀请进群的用户ID", required = true)})
    public R<?> inviteIntoGroup(@RequestBody JSONObject param) {
        try {
            Long groupId = param.getLong("groupId");
            Long userId = param.getLong("userId");
            return imGroupService.inviteIntoGroup(groupId,userId);
        } catch (Exception e) {
            log.error("error", e);
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 群主以及群管理可用的信息编辑
     * @return
     */
    @PostMapping("infoEdit")
    @ApiOperation("群主以及群管理可见的信息编辑")
    public R<?> infoEdit(@RequestBody GroupInfoDto groupInfoDto){
        try {
            imGroupService.infoEdit(groupInfoDto);
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            log.error("error",e);
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 群成员对群的基本信息设置
     * @return
     */
    @ApiOperation("群成员对群的基本信息设置")
    @PostMapping("memberGroupInfoEdit")
    public R<?> memberGroupInfoEdit(@RequestBody MemberGroupInfoEditDto groupInfoEditDto ){
        try {
            groupMemberService.memberGroupInfoEdit(groupInfoEditDto);
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            log.error("error",e);
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 解散群
     * @return
     */
    @ApiOperation("解散群")
    @PostMapping("disband")
    @ApiImplicitParams({@ApiImplicitParam(name = "groupId", value = "解散的群ID", required = true)})
    public R<?> disband(@RequestBody JSONObject groupId){
        try {
            imGroupService.disband(groupId.getLong("groupId"));
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            log.error("error",e);
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

}
